bool hasCycle(ListNode *head) 
{
    if(!head || !head->next) return false;

    ListNode *fast = head->next, *slow = head;

    while(fast != slow)
    {
        if(!fast || !fast->next) return false;
        fast = fast->next->next;
        slow = slow->next;
    }
    return true;
}